layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 管理
     */
    var btyPaymentOrderDetailsManualOperation = {
        tableId: "btyPaymentOrderDetailsManualOperationTable"
    };

    /**
     * 初始化表格的列
     */
    btyPaymentOrderDetailsManualOperation.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {field: 'yybbId', sort: true, title: '日现金表id'},
            {field: 'yyr', sort: true, title: '营业日期'},
            {field: 'dm', sort: true, title: '店名'},
            {field: 'yaoma', sort: true, title: '妖码'},
            {field: 'paidCash', sort: true, title: '现金实缴'},
            {field: 'payCode', sort: true, title: '支付编号'},
            {field: 'btyPaymentOrderId', sort: true, title: '支付订单表id'},
            {field: 'payState', sort: true, title: '支付状态（0.支付中，1，支付成功，2支付为失败）'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'createUser', sort: true, title: '创建人'},
            {field: 'updateTime', sort: true, title: '修改时间'},
            {field: 'updateUser', sort: true, title: '修改人'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    btyPaymentOrderDetailsManualOperation.search = function () {
        var queryData = {};


        table.reload(btyPaymentOrderDetailsManualOperation.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    btyPaymentOrderDetailsManualOperation.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/btyPaymentOrderDetailsManualOperation/add'
    };

    /**
     * 跳转到编辑页面
     *
     * @param data 点击按钮时候的行数据
     */
    btyPaymentOrderDetailsManualOperation.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/btyPaymentOrderDetailsManualOperation/edit?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    btyPaymentOrderDetailsManualOperation.exportExcel = function () {
        var checkRows = table.checkStatus(btyPaymentOrderDetailsManualOperation.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    btyPaymentOrderDetailsManualOperation.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyPaymentOrderDetails/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(btyPaymentOrderDetailsManualOperation.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    /**
     * 点击增加现金实缴
     *
     */
    btyPaymentOrderDetailsManualOperation.btnAddPaidCash = function(){

        var id = $("#id").val();
        var xj = $("#xj").val();
        var xjbz = $("#xjbz").val();
        var sumTodayCash = $("#sumTodayCash").val();
        var yaoma = $("#yaoma").val();
        var yyr = $("#yyr").val();
        var paidCash = $("#paidCash").val();

        if(sumTodayCash!=paidCash){
            Feng.error("输入的现金实缴("+paidCash+")，和店内应现金实缴("+sumTodayCash+")不一致。请确认现金实缴金额。");
            return;
        }

        var operation = function () {
            var queryData = {};
            queryData["id"] = id;
            queryData["xj"] = xj;
            queryData["xjbz"] = xjbz;
            queryData["sumTodayCash"] = sumTodayCash;
            queryData["yaoma"] = yaoma;
            queryData["yyr"] = yyr;
            queryData["paidCash"] = paidCash;

            $.ajax({
                type: "post",
                url: "/btyPaymentOrderDetails/btyPaymentOrderDetailsForPaidCash",
                datatype: "json",
                async: false,
                data:queryData,
                success: function (data) {
                    if(data.success == true){
                        Feng.success("成功!");
                    }else{
                        Feng.error(data.data);
                    }
                    console.log(data);
                    //传给上个页面，刷新table用
                    admin.putTempData('formOk', true);
                    //关掉对话框
                    admin.closeThisDialog();
                },
                error: function (data) {
                    Feng.error("失败!");
                }
            });

        };
        Feng.confirm("确认增加现金实缴?", operation);
    }

    // // 渲染表格
    // var tableResult = table.render({
    //     elem: '#' + btyPaymentOrderDetailsManualOperation.tableId,
    //     url: Feng.ctxPath + '/btyPaymentOrderDetailsManualOperation/list',
    //     page: true,
    //     height: "full-158",
    //     cellMinWidth: 100,
    //     cols: btyPaymentOrderDetailsManualOperation.initColumn()
    // });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        btyPaymentOrderDetailsManualOperation.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        btyPaymentOrderDetailsManualOperation.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        btyPaymentOrderDetailsManualOperation.exportExcel();
    });

    // 导出excel
    $('#btnAddPaidCash').click(function () {
        btyPaymentOrderDetailsManualOperation.btnAddPaidCash();
    });


    // 工具条点击事件
    table.on('tool(' + btyPaymentOrderDetailsManualOperation.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            btyPaymentOrderDetailsManualOperation.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            btyPaymentOrderDetailsManualOperation.onDeleteItem(data);
        }

    });
});
